<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. AOP入门1). 创建项目并导入Jar包：spring-beans-5.0.5.RELEASE.jar、spring-context-5.0.5.RELEASE.jar、spring-core-5.0.5.RELEASE.jar、spring-expression-5.0.5.RELEASE.jar、spring-aop-5.0.5.RELEASE.jar、spring-aop-5.">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaEE-Spring-AOP使用">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JavaEE-Spring-AOP使用/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. AOP入门1). 创建项目并导入Jar包：spring-beans-5.0.5.RELEASE.jar、spring-context-5.0.5.RELEASE.jar、spring-core-5.0.5.RELEASE.jar、spring-expression-5.0.5.RELEASE.jar、spring-aop-5.0.5.RELEASE.jar、spring-aop-5.">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-84b7822efce5449d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-a18498c423b1ec32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-cf274ee28371b10e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9066ba6d3956ea9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-e52c9af3653d7701.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9c5af9b02cf0d61a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0a0ab53a8772a214.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-3587bbf62cfef1de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-ff3a40f743734375.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-97da609ea3899e71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-590595a53f2c5145.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5569f8e07e646ca0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:51:36.561Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaEE-Spring-AOP使用">
<meta name="twitter:description" content="####1. AOP入门1). 创建项目并导入Jar包：spring-beans-5.0.5.RELEASE.jar、spring-context-5.0.5.RELEASE.jar、spring-core-5.0.5.RELEASE.jar、spring-expression-5.0.5.RELEASE.jar、spring-aop-5.0.5.RELEASE.jar、spring-aop-5.">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-84b7822efce5449d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JavaEE-Spring-AOP使用/"/>





  <title>JavaEE-Spring-AOP使用 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/JakartaEE-Framework/JavaEE-Spring-AOP使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JavaEE-Spring-AOP使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. AOP入门<br>1). 创建项目并导入Jar包：spring-beans-5.0.5.RELEASE.jar、spring-context-5.0.5.RELEASE.jar、spring-core-5.0.5.RELEASE.jar、spring-expression-5.0.5.RELEASE.jar、spring-aop-5.0.5.RELEASE.jar、spring-aop-5.0.5.RELEASE.jar、spring-aspects-5.0.5.RELEASE.jar、<a href="http://commons.apache.org/proper/commons-logging/" target="_blank" rel="noopener">commons-logging-1.2.jar</a>、<a href="http://www.mvnrepository.com/artifact/aopalliance/aopalliance" target="_blank" rel="noopener">aopalliance-1.0.jar</a>、<a href="http://www.mvnrepository.com/artifact/org.aspectj/aspectjweaver" target="_blank" rel="noopener">aspectjweaver-1.8.13.jar</a>.<br><img src="https://upload-images.jianshu.io/upload_images/3110861-84b7822efce5449d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><a id="more"></a></p>
<p>2). 引入aop命令空间<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans</span><br><span class="line">        http://www.springframework.org/schema/beans/spring-beans.xsd</span><br><span class="line">        http://www.springframework.org/schema/context</span><br><span class="line">        http://www.springframework.org/schema/context/spring-context-4.2.xsd</span><br><span class="line">        http://www.springframework.org/schema/aop </span><br><span class="line">        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-a18498c423b1ec32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>3). 创建PersonService接口<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public interface PersonService &#123;</span><br><span class="line">	public void save(String name);</span><br><span class="line">	public void update(String name, int id);</span><br><span class="line">	public String getPersonName(int id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>4). 创建实现了PersonService的接口PersonServiceImpl<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public class PersonServiceImpl implements PersonService&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void save(String name) &#123;</span><br><span class="line">		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void update(String name, int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是update方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getPersonName(int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是getPersonName方法&quot;);</span><br><span class="line">		return &quot;mazaiting&quot;;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>5). 创建切面类——MyInterceptor<br>最典型的切入点表达式是根据方法的签名来匹配各种方法：</p>
<ul>
<li><code>execution (* cn.itcast.service.impl.PersonServiceImpl.*(..))</code>：匹配PersonServiceImpl类中声明的所有方法。第一个<em>代表任意修饰符及任意返回值类型，第二个</em>代表任意方法，..匹配任意数量任意类型的参数，若目标类与该切面在同一个包中，可以省略包名。</li>
<li><code>execution public * cn.itcast.service.impl.PersonServiceImpl.*(..)</code>：匹配PersonServiceImpl类中的所有公有方法。</li>
<li><code>execution public double cn.itcast.service.impl.PersonServiceImpl.*(..)</code>：匹配PersonServiceImpl类中返回值类型为double类型的所有公有方法。</li>
<li><code>execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, ..)</code>：匹配PersonServiceImpl类中第一个参数为double类型，后面不管有无参数的所有公有方法，并且该方法的返回值类型为double类型。</li>
<li><code>execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, double)</code>：匹配PersonServiceImpl类中参数类型为double，double类型的，并且返回值类型也为double类型的所有公有方法。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAccessCheck() &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><strong>注意：若是将一个类声明为一个切面，那么需要把该类放到IOC容器管理。</strong><br>6). 在bean.xml文件中添加配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans</span><br><span class="line">        http://www.springframework.org/schema/beans/spring-beans.xsd</span><br><span class="line">        http://www.springframework.org/schema/context</span><br><span class="line">        http://www.springframework.org/schema/context/spring-context-4.2.xsd</span><br><span class="line">        http://www.springframework.org/schema/aop </span><br><span class="line">        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd&quot;&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;aop:aspectj-autoproxy /&gt;</span><br><span class="line">	&lt;bean id=&quot;myInterceptor&quot; class=&quot;com.mazaiting.test.MyInterceptor&quot;&gt;&lt;/bean&gt;</span><br><span class="line">	&lt;bean id=&quot;personService&quot; class=&quot;com.mazaiting.test.service.impl.PersonServiceImpl&quot;&gt;&lt;/bean&gt;</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p>7). 创建测试类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class TestAop &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void test() &#123;</span><br><span class="line">		// 加载配置文件</span><br><span class="line">		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;bean.xml&quot;);</span><br><span class="line">		// 创建对象</span><br><span class="line">		PersonService service = (PersonService) context.getBean(&quot;personService&quot;);</span><br><span class="line">		service.save(&quot;xxx&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>8). 执行结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-cf274ee28371b10e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>####2. AOP细节(前置通知、后置通知、异常通知、最终通知、环绕通知等)<br>1). 添加后置通知<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAccessCheck() &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(&quot;anyMethod()&quot;)</span><br><span class="line">	public void	doAfterRunning() &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-9066ba6d3956ea9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>2). 添加最终通知<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAccessCheck() &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(&quot;anyMethod()&quot;)</span><br><span class="line">	public void	doAfterRunning() &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-e52c9af3653d7701.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>3). 添加异常通知<br>I. 修改PersonServiceImpl中的save代码实现<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class PersonServiceImpl implements PersonService&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void save(String name) &#123;</span><br><span class="line">		throw new RuntimeException(&quot;异常&quot;);</span><br><span class="line">//		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void update(String name, int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是update方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getPersonName(int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是getPersonName方法&quot;);</span><br><span class="line">		return &quot;mazaiting&quot;;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 修改MyInterceptor类中代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAccessCheck() &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(&quot;anyMethod()&quot;)</span><br><span class="line">	public void	doAfterRunning() &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个异常通知</span><br><span class="line">	@AfterThrowing(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfterThrowing() &#123;</span><br><span class="line">		System.out.println(&quot;异常通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 执行测试代码，打印结果:<br><img src="https://upload-images.jianshu.io/upload_images/3110861-9c5af9b02cf0d61a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>4). 添加环绕通知<br>I. 将代码重置回第二步结束时样子<br>II. 修改MyInterceptor文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAccessCheck() &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(&quot;anyMethod()&quot;)</span><br><span class="line">	public void	doAfterRunning() &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个异常通知</span><br><span class="line">	@AfterThrowing(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfterThrowing() &#123;</span><br><span class="line">		System.out.println(&quot;异常通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 环绕通知，环绕通知方法的写法是固定</span><br><span class="line">	@Around(&quot;anyMethod()&quot;)</span><br><span class="line">	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable&#123;</span><br><span class="line">		System.out.println(&quot;进入方法&quot;);</span><br><span class="line">		Object result = pjp.proceed();</span><br><span class="line">		System.out.println(&quot;退出方法&quot;);</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>#####注意：环绕通知内部一定要确保执行proceed()该方法，如果不执行该方法，业务bean中被拦截的方法就不会被执行。当执行该方法，如果后面还有切面的话，它的执行顺序应该是这样的：先执行后面的切面，如果后面没有切面了，再执行最终的目标对象的业务方法。若不执行该方法，则后面的切面，业务bean的方法都不会被执行。 其实我们仅使用环绕通知就可以实现前置通知、后置通知、异常通知、最终通知等的效果。<br>III. 执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-0a0ab53a8772a214.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>####3. 前置通知获取参数<br>I. 修改MyInterceptor中的doAccessCheck方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod() &amp;&amp; args(name)&quot;)</span><br><span class="line">	public void doAccessCheck(String name) &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot; + name);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(&quot;anyMethod()&quot;)</span><br><span class="line">	public void	doAfterRunning() &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个异常通知</span><br><span class="line">	@AfterThrowing(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfterThrowing() &#123;</span><br><span class="line">		System.out.println(&quot;异常通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 环绕通知，环绕通知方法的写法是固定</span><br><span class="line">	@Around(&quot;anyMethod()&quot;)</span><br><span class="line">	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable&#123;</span><br><span class="line">		System.out.println(&quot;进入方法&quot;);</span><br><span class="line">		Object result = pjp.proceed();</span><br><span class="line">		System.out.println(&quot;退出方法&quot;);</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-3587bbf62cfef1de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>####4. 获取getPersonName()方法的返回参数<br>I. 修改MyInterceptor代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod() &amp;&amp; args(name)&quot;)</span><br><span class="line">	public void doAccessCheck(String name) &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot; + name);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(pointcut = &quot;anyMethod()&quot;, returning = &quot;result&quot;)</span><br><span class="line">	public void	doAfterRunning(String result) &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot; + result);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个异常通知</span><br><span class="line">	@AfterThrowing(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfterThrowing() &#123;</span><br><span class="line">		System.out.println(&quot;异常通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 环绕通知，环绕通知方法的写法是固定</span><br><span class="line">	@Around(&quot;anyMethod()&quot;)</span><br><span class="line">	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable&#123;</span><br><span class="line">		System.out.println(&quot;进入方法&quot;);</span><br><span class="line">		Object result = pjp.proceed();</span><br><span class="line">		System.out.println(&quot;退出方法&quot;);</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 修改测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class TestAop &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void test() &#123;</span><br><span class="line">		// 加载配置文件</span><br><span class="line">		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;bean.xml&quot;);</span><br><span class="line">		// 创建对象</span><br><span class="line">		PersonService service = (PersonService) context.getBean(&quot;personService&quot;);</span><br><span class="line">//		service.save(&quot;xxx&quot;);</span><br><span class="line">		service.getPersonName(2);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-ff3a40f743734375.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>####5. 获取异常<br>I. 修改PersonServiceImpl代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class PersonServiceImpl implements PersonService&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void save(String name) &#123;</span><br><span class="line">		throw new RuntimeException(&quot;异常&quot;);</span><br><span class="line">//		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void update(String name, int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是update方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getPersonName(int id) &#123;</span><br><span class="line">		System.out.println(&quot;我是getPersonName方法&quot;);</span><br><span class="line">		return &quot;mazaiting&quot;;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 修改异常通知代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">// 声明当前类为切面类</span><br><span class="line">@Aspect</span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">	// 声明一个切入点，anyMethod为切入点名称</span><br><span class="line">	@Pointcut(&quot;execution (* com.mazaiting.test.service.impl.PersonServiceImpl.*(..))&quot;)</span><br><span class="line">	private void anyMethod()&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明该方法是一个前置通知，在目标方法开始之前执行</span><br><span class="line">	@Before(&quot;anyMethod() &amp;&amp; args(name)&quot;)</span><br><span class="line">	public void doAccessCheck(String name) &#123;</span><br><span class="line">		System.out.println(&quot;前置通知&quot; + name);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个后置通知</span><br><span class="line">	@AfterReturning(pointcut = &quot;anyMethod()&quot;, returning = &quot;result&quot;)</span><br><span class="line">	public void	doAfterRunning(String result) &#123;</span><br><span class="line">		System.out.println(&quot;后置通知&quot; + result);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个最终通知</span><br><span class="line">	@After(&quot;anyMethod()&quot;)</span><br><span class="line">	public void doAfter() &#123;</span><br><span class="line">		System.out.println(&quot;最终通知&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 声明一个异常通知</span><br><span class="line">	@AfterThrowing(pointcut = &quot;anyMethod()&quot;, throwing = &quot;e&quot;)</span><br><span class="line">	public void doAfterThrowing(Exception e) &#123;</span><br><span class="line">		System.out.println(&quot;异常通知&quot; + e);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 环绕通知，环绕通知方法的写法是固定</span><br><span class="line">	@Around(&quot;anyMethod()&quot;)</span><br><span class="line">	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable&#123;</span><br><span class="line">		System.out.println(&quot;进入方法&quot;);</span><br><span class="line">		Object result = pjp.proceed();</span><br><span class="line">		System.out.println(&quot;退出方法&quot;);</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 修改测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class TestAop &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void test() &#123;</span><br><span class="line">		// 加载配置文件</span><br><span class="line">		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;bean.xml&quot;);</span><br><span class="line">		// 创建对象</span><br><span class="line">		PersonService service = (PersonService) context.getBean(&quot;personService&quot;);</span><br><span class="line">		service.save(&quot;xxx&quot;);</span><br><span class="line">//		service.getPersonName(2);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-97da609ea3899e71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p>####6. 配置文件实现AOP<br>1). 创建Person接口<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface Person &#123;</span><br><span class="line">	void save();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>2). 创建实现Person接口的PersonImpl类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public class PersonImpl implements Person &#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void save() &#123;</span><br><span class="line">		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>3). 创建MyInterceptor类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public class MyInterceptor &#123;</span><br><span class="line">    public void doAccessCheck() &#123;</span><br><span class="line">        System.out.println(&quot;前置通知&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void doAfterReturning() &#123; </span><br><span class="line">        System.out.println(&quot;后置通知&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void doAfter() &#123;</span><br><span class="line">        System.out.println(&quot;最终通知&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void doAfterThrowing() &#123;</span><br><span class="line">        System.out.println(&quot;异常通知&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable &#123;</span><br><span class="line">        System.out.println(&quot;进入方法&quot;);</span><br><span class="line">        Object result = pjp.proceed();</span><br><span class="line">        System.out.println(&quot;退出方法&quot;);</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>4). 修改bean.xml，添加配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans</span><br><span class="line">        http://www.springframework.org/schema/beans/spring-beans.xsd</span><br><span class="line">        http://www.springframework.org/schema/context</span><br><span class="line">        http://www.springframework.org/schema/context/spring-context-4.2.xsd</span><br><span class="line">        http://www.springframework.org/schema/aop </span><br><span class="line">        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd&quot;&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;aop:aspectj-autoproxy /&gt;</span><br><span class="line">	&lt;bean id=&quot;myInterceptor1&quot; class=&quot;com.mazaiting.test1.MyInterceptor&quot;&gt;&lt;/bean&gt;</span><br><span class="line">	&lt;bean id=&quot;person&quot; class=&quot;com.mazaiting.test1.service.impl.PersonImpl&quot;&gt;&lt;/bean&gt;</span><br><span class="line">	&lt;aop:config&gt;</span><br><span class="line">		&lt;aop:aspect id=&quot;asp&quot; ref=&quot;myInterceptor1&quot;&gt;</span><br><span class="line">			&lt;aop:pointcut expression=&quot;execution(* com.mazaiting.test1.service.impl.PersonImpl.*(..))&quot; id=&quot;mycut&quot;/&gt;</span><br><span class="line">			&lt;aop:before pointcut-ref=&quot;mycut&quot; method=&quot;doAccessCheck&quot;/&gt;</span><br><span class="line">			&lt;aop:after pointcut-ref=&quot;mycut&quot;	method=&quot;doAfter&quot;/&gt;</span><br><span class="line">			&lt;aop:after-returning pointcut-ref=&quot;mycut&quot; method=&quot;doAfterReturning&quot;/&gt;</span><br><span class="line">			&lt;aop:after-throwing pointcut-ref=&quot;mycut&quot; method=&quot;doAfterThrowing&quot;/&gt;</span><br><span class="line">			&lt;aop:around pointcut-ref=&quot;mycut&quot; method=&quot;doBasicProfiling&quot;/&gt;</span><br><span class="line">		&lt;/aop:aspect&gt;</span><br><span class="line">	&lt;/aop:config&gt;</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p>5). 创建测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class TestPerson &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void test() &#123;</span><br><span class="line">		// 加载配置文件</span><br><span class="line">		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;bean.xml&quot;);</span><br><span class="line">		// 创建对象</span><br><span class="line">		Person person = (Person) context.getBean(&quot;person&quot;);</span><br><span class="line">		person.save();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>6). 执行测试代码，打印结果:<br><img src="https://upload-images.jianshu.io/upload_images/3110861-590595a53f2c5145.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"></p>
<p>7). 修改PersonImpl代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class PersonImpl implements Person &#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void save() &#123;</span><br><span class="line">//		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">		throw new RuntimeException(&quot;yichang&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>8). 执行测试代码，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5569f8e07e646ca0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图12.png"></p>
<p>###<a href="https://download.csdn.net/download/mazaiting/10352101" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-Spring+Mybatis整合/" rel="next" title="JavaEE-Spring+Mybatis整合">
                <i class="fa fa-chevron-left"></i> JavaEE-Spring+Mybatis整合
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-Spring-IoC入门/" rel="prev" title="JavaEE-Spring-IoC入门">
                JavaEE-Spring-IoC入门 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
